import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.*;

public class TextExtractor {
	
	public static void main(String[] argv) throws Throwable {
		if (argv.length < 3) {
            System.err.println("Not enough arguments were given");
            return;
        }
        File srcDir = new File(argv[0]);
		File dstDir = new File(argv[1]);
		if (!dstDir.exists())
			dstDir.mkdir();
		String encoding = argv[2];
		extractFromDir(srcDir, dstDir, encoding);
	}
	
	public static void extractFromFile(File srcFile, File dstFile, String encoding) 
			throws Throwable {
		CorpusParser srcParser = new CorpusParser(srcFile);
		FileOutputStream dstStream = new FileOutputStream(dstFile);
		OutputStreamWriter dstWriter = new OutputStreamWriter(dstStream, encoding);
		BufferedWriter dst = new BufferedWriter(dstWriter);
		
		NodeList sentences = srcParser.getSentences();
		try {
			for (int i = 0; i < sentences.getLength(); i++) {
				Element sentence = (Element) sentences.item(i);
				NodeList tokens = sentence.getChildNodes();
				for (int j = 0; j < tokens.getLength(); j++) {
					Element token = (Element) tokens.item(j);
					String surface = token.getAttribute("surface");
					dst.write(surface);
					dst.write(" ");
				}
			}
		} finally {
			dst.close();
		}
		
	}
	
	public static void extractFromDir(File srcDir, File dstDir, String encoding) 
	throws Throwable {
		// Assume *.xml files contain tagged text 
		FilenameFilter filter = new FilenameFilter() {
	        public boolean accept(File dir, String name) {
	            return name.endsWith(".xml");
	        }
	    };
	    File[] srcFiles = srcDir.listFiles(filter);
		for (File srcFile : srcFiles) {
			File dstFile = new File(dstDir, srcFile.getName().replaceAll("xml$", "txt"));
			System.out.println("Extracting from " + srcFile.getName());
			extractFromFile(srcFile, dstFile, encoding);
		}
	}
	
}
